package edu.hniu.skilltest.demo05;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import edu.hniu.skilltest.R;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btnBack,btnRegister;
    private EditText etEmail, etNickname, etPwd, etPwdAgain;
    public static Pattern p = Pattern
            .compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register2);
        initView();
    }

    private void initView() {
        btnRegister = findViewById(R.id.btn_register_05);
        btnBack = findViewById(R.id.btn_back_05);
        etEmail = findViewById(R.id.et_email_05);
        etNickname = findViewById(R.id.et_nickname_05);
        etPwd = findViewById(R.id.et_pwd_05);
        etPwdAgain = findViewById(R.id.et_pwd_again_05);
        btnBack.setOnClickListener(this);
        btnRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_back_05:
                finish();//返回主页
                break;
            case R.id.btn_register_05://点击注册
                String email = etEmail.getText().toString().trim();
                String nickName = etNickname.getText().toString().trim();
                String pwd = etPwd.getText().toString().trim();
                String pwdAgain = etPwdAgain.getText().toString().trim();

                if (TextUtils.isEmpty(email) || TextUtils.isEmpty(nickName)
                        || TextUtils.isEmpty(pwd) || TextUtils.isEmpty(pwdAgain)){
                    showToast(this, "请输入注册的必填项信息!");
                    break;
                }

                if (!(isEmail(email) && email.length() <= 31)){
                    showToast(this, "邮箱格式错误!");
                    break;
                }
                if ( !(pwd.equals(pwdAgain))){
                    showToast(this, "两次密码输入不一致!");
                }else {
                    showToast(this, "注册信息输入正确!");
                }
                break;
        }
    }

    public static void showToast(Context context, String text){
        Toast.makeText(context, text, Toast.LENGTH_LONG).show();
    }

    public static boolean isEmail(String email) {
        if (email == null || "".equals(email)) {
            return false;
        }
        Matcher matcher = p.matcher(email);
        return matcher.matches();
    }
}